<script setup lang="ts">
import { usePurchaseUser } from "./utils/hook";
import { ref, computed, nextTick, onMounted } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import forms, { type FormProps } from "./import.vue";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
import {
  delay,
  subBefore,
  deviceDetection,
  useResizeObserver
} from "@pureadmin/utils";

// import Database from "@iconify-icons/ri/database-2-line";
// import More from "@iconify-icons/ep/more-filled";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import Menu from "@iconify-icons/ep/menu";
import AddFill from "@iconify-icons/ri/add-circle-line";
import Close from "@iconify-icons/ep/close";
import Check from "@iconify-icons/ep/check";

defineOptions({
  name: "SystemPurchaseUser"
});


const treeRef = ref();
const formRef = ref();
const tableRef = ref();
const contentRef = ref();
const treeHeight = ref();

const {
  form, isShow, curRow, loading, columns,
  rowStyle,
  dataList, treeData,treeProps, isLinkage, pagination, isExpandAll, isSelectAll, treeSearchValue,
  // buttonClass,
  onSearch,userList,
  resetForm,
  openDialog,
  handleMenu,
  handleSave,
  handleDelete,
  filterMethod,
  transformI18n,
  onQueryChanged,
  // handleDatabase,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange
} = usePurchaseUser(treeRef);

onMounted(() => {
  useResizeObserver(contentRef, async () => {
    await nextTick();
    delay(60).then(() => {
      treeHeight.value = parseFloat(
        subBefore(tableRef.value.getTableDoms().tableWrapper.style.height, "px")
      );
    });
  });
});
</script>

<template>
  <div class="main">
    <el-form ref="formRef" :inline="true" :model="form" class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto">
      <el-form-item label="" prop="user_id">
        <el-select v-model="form.user_id" placeholder="选择人员" clearable filterable class="!w-[180px]">
          <el-option :label="item.username" :value="item.id"  v-for="item in userList" />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="status">
        <el-select v-model="form.status" placeholder="请选择状态" clearable class="!w-[180px]">
          <el-option label="已启用" value="1" />
          <el-option label="已停用" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="useRenderIcon('ri:search-line')" :loading="loading" @click="onSearch"> {{ t("buttons.search") }}</el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">{{ t("buttons.reset") }}</el-button>
      </el-form-item>
    </el-form>

    <div ref="contentRef" :class="['flex', deviceDetection() ? 'flex-wrap' : '']">
      <PureTableBar :class="[isShow && !deviceDetection() ? '!w-[60vw]' : 'w-full']" style="transition: width 220ms cubic-bezier(0.4, 0, 0.2, 1)"
        title="采购人员额度" :columns="columns"
        @refresh="onSearch"
      >
        <template #buttons>
          <el-button v-perms="'PurchaseUser:btn:add'"  type="primary" :icon="useRenderIcon(AddFill)" @click="openDialog()">
            {{ t("buttons.add") }}
          </el-button>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <pure-table ref="tableRef" align-whole="center" showOverflowTooltip
            table-layout="auto" :loading="loading"
            :size="size" adaptive :row-style="rowStyle"
            :adaptiveConfig="{ offsetBottom: 108 }"
            :data="dataList"
            :columns="dynamicColumns"
            :pagination="{ ...pagination, size }"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button class="reset-margin"  link v-perms="'PurchaseUser:btn:edit'" type="primary"  :size="size"
                :icon="useRenderIcon(EditPen)" @click="openDialog('编辑', row)"
              >
                {{ t("buttons.edit") }}
              </el-button>
              <el-popconfirm :title="t('buttons.deleteConfirm')" @confirm="handleDelete(row)" >
                <template #reference>
                  <el-button v-perms="'PurchaseUser:btn:delete'"  class="reset-margin" link type="primary" :size="size" :icon="useRenderIcon(Delete)">
                    {{ t("buttons.delete") }}
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>


</template>

<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
